﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using Dsl.Ped.Common.Commands;
using Dsl.Ped.Framework.Extensions;

namespace Dsl.Ped.Framework.WPFControls.KeyActionSelector
{
    /// <summary>
    /// Interaction logic for KeyActionUserControl.xaml
    /// 
    /// This object implements the UserControl, which reacts on a defined Key by invoking
    /// the context menu of defined actions to execute
    /// </summary>
    public partial class KeyActionSelectorUserControl : UserControl
    {
        /// <summary>
        /// DependencyProperty of a key which invokes opening of a context menu selector
        /// </summary>
        public static readonly DependencyProperty KeyProperty;
        public Key Key
        {
            get
            {
                return (Key)GetValue(KeyProperty);
            }
            set
            {
                SetValue(KeyProperty, value);
            }
        }

        /// <summary>
        /// DependencyProperty of a collection of menu items like options
        /// </summary>
        public static readonly DependencyProperty ItemsSourceProperty;
        public IEnumerable<IMenuCommand> ItemsSource
        {
            get
            {
                return (IEnumerable<IMenuCommand>)GetValue(ItemsSourceProperty);
            }
            set
            {
                SetValue(ItemsSourceProperty, value);
            }
        }

        /// <summary>
        /// Static constructor, register DependencyProperties
        /// </summary>
        static KeyActionSelectorUserControl()
        {
            KeyProperty = DependencyProperty.Register(
                "Key", typeof(Key), typeof(KeyActionSelectorUserControl));

            ItemsSourceProperty = DependencyProperty.Register(
                "ItemsSource",
                typeof(IEnumerable<IMenuCommand>),
                typeof(KeyActionSelectorUserControl),
                new FrameworkPropertyMetadata(
                    new PropertyChangedCallback(onUpdateItemsSource)));
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public KeyActionSelectorUserControl()
        {
            InitializeComponent();

            textBox.PreviewKeyDown += new KeyEventHandler(onPreviewKeyDown);
        }

        /// <summary>
        /// When the Key is clicked it is checked if the Key was defined to invoke
        /// the ContextMenu's selection of an option
        /// </summary>
        /// <param name="sender">This user control</param>
        /// <param name="e">Arguments of the event</param>
        private void onPreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key)
            {
                if (!ItemsSource.IsNullOrEmpty<IMenuCommand>())
                {
                    contextMenu.PlacementTarget = textBox;
                    contextMenu.Placement = PlacementMode.Relative;
                    contextMenu.IsOpen = true;
                }
            }

            if (e.Key != Key.Tab)
            {
                e.Handled = true;
            }
        }

        /// <summary>
        /// When the ItemsSource property is changed this method is invoked and
        /// reloads the colleciton of items in the selection
        /// </summary>
        /// <param name="sender">This user control</param>
        /// <param name="e">Arguments of the event</param>
        private static void onUpdateItemsSource(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            // Get the user control to set
            var keyActionSelector = sender as KeyActionSelectorUserControl;
            keyActionSelector.contextMenu.ItemsSource = (IEnumerable<IMenuCommand>)e.NewValue;
        }
    }
}
